﻿namespace HiMap.MapControls.Tools
{
    using HiMap.Carto;
    using HiMap.Geometry;
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public class ToolDistance : ToolClass
    {
        private double Distance;
        private Point pt1 = new Point();
        private Point pt2 = new Point();

        protected override void OnMouseDown(object sender, MouseEventArgs e)
        {
            Map map = base.Map.Map;
            if ((this.pt1.X == 0) && (this.pt1.Y == 0))
            {
                this.pt1.X = e.X;
                this.pt1.Y = e.Y;
            }
            else
            {
                if ((this.pt2.X == 0) && (this.pt2.Y == 0))
                {
                    this.pt2.X = e.X;
                    this.pt2.Y = e.Y;
                }
                else
                {
                    this.pt1.X = this.pt2.X;
                    this.pt1.Y = this.pt2.Y;
                    this.pt2.X = e.X;
                    this.pt2.Y = e.Y;
                }
                base.Map.DrawLine(new Pen(Color.Red), this.pt1, this.pt2);
                double num = 0;
                if (base.Map.Map.Pro == null)
                {
                    num = map.Distance(base.Map.PointToMapPoint(this.pt1.X, this.pt1.Y), base.Map.PointToMapPoint(this.pt2.X, this.pt2.Y));
                }
                else
                {
                    IPoint point = base.Map.PointToMapPoint(this.pt1.X, this.pt1.Y);
                    IPoint point2 = base.Map.PointToMapPoint(this.pt2.X, this.pt2.Y);
                    num = Math.Sqrt(Math.Pow(point.X - point2.X, 2) + Math.Pow(point.Y - point2.Y, 2));
                }
                this.Distance += num;
                this.Distance = Math.Round(this.Distance, 2);
                Brush brush = new SolidBrush(Color.Blue);
                base.Map.DrawString(this.Distance.ToString() + "m", new Font("Arial", 10f, FontStyle.Regular), brush, (float) e.X, (float) e.Y);
            }
        }
    }
}

